﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Tomahawk.Serialization;
using Tomahawk.Serialization.SerialBoost;

namespace Tomahawk.Runtime.Logic.UI
{
    public class XStyleManager : XObject
    {
        public XStyleManager()
        {
            textStyles = new Dictionary<string, TextStyle>();
        }

        #region "TextStyle"

        public struct FontColor
        {
            [XmlAttribute("r")]
            public byte R;
            
            [XmlAttribute("g")]
            public byte G;
            
            [XmlAttribute("b")]
            public byte B;

            [XmlAttribute("a")]
            public byte A;
        }

        public enum TextVAlign
        {
            Top,
            Centered,
            Bottom
        }

        public enum TextHAlign
        {
            Left,
            Centered,
            Right
        }

        public struct TextAlign
        {
            [XmlAttribute("vertical")]
            public TextVAlign VerticalAlign;
            
            [XmlAttribute("horizontal")]
            public TextHAlign HorizontalAlign;
        }

        public struct TextMargin
        {
            [XmlAttribute("vertical")]
            public int Vertical;

            [XmlAttribute("horizontal")]
            public int Horizontal;
        }

        public struct ShadowProperties
        {
            [XmlAttribute("enabled")]
            public bool Enabled;

            [XmlAttribute("verticalPos")]
            public int VerticalDisplacement;

            [XmlAttribute("horizontalPos")]
            public int HorizontalDisplacement;
        }

        /// <summary>
        /// Text style definition class
        /// </summary>
        public class TextStyle
        {
            [XmlAttribute("name")]
            public string Name;

            [XmlElement("Font")]
            public string FontAsset;

            [XmlElement("Align")]
            public TextAlign Alignment;

            [XmlElement("Margin")]
            public TextMargin Margin;
            
            [XmlElement("DefaultColor")]
            public FontColor DefaultColor;

            [XmlElement("SelectedColor")]
            public FontColor SelectedColor;

            [XmlElement("SelectedAltColor")]
            public FontColor SelectedAltColor;

            [XmlElement("PressedColor")]
            public FontColor PressedColor;

            [XmlElement("ShadowColor")]
            public FontColor ShadowColor;

            [XmlElement("Shadow")]
            public ShadowProperties Shadow;

        }

        /// <summary>
        /// Stores all the text styles found on a dictionary, indexed by their names
        /// </summary>
        private void indexTextStyles()
        {
            foreach (TextStyle style in this.TextStyleList)
            {
                if ((style.Name != null) && (style.Name != ""))
                {
                    try
                    {
                        this.textStyles.Add(style.Name, style);
                    }
                    catch (Exception)
                    {
                        Engine.Instance.DebugConsole.Warn(
                            string.Format("Duplicated name style '{0}' found on config file.", style.Name));
                    }
                }
                else
                {
                    Engine.Instance.DebugConsole.Warn("WARNING: Unnamed style found on config file!");
                }
            }
        }

        #endregion

        #region "Serializable properties"

        [XmlElement("TextStyles")]
        public SerializableList<TextStyle> TextStyleList;

        #endregion

        #region "Public accesors"

        /// <summary>
        /// Get a text style by its name
        /// </summary>
        public TextStyle GetTextStyle(string textStyleName)
        {
            TextStyle retval = null;
            try
            {
                retval = this.textStyles[textStyleName];
            }
            catch (Exception)
            {
                Engine.Instance.DebugConsole.Warn("TextStyle not defined: " + textStyleName);
            }
            return retval;
        }

        #endregion

        /// <summary>
        /// Stores all the textstyles found
        /// </summary>
        private Dictionary<String, TextStyle> textStyles;

        /// <summary>
        /// Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            //indexes text styles by their names
            this.indexTextStyles();
        }

        public override void OnBeforeDestroy()
        {
            base.OnBeforeDestroy();
        }

    }

}
